package t1012;

import java.util.Scanner;

/**
 * @姓名 张东升
 * @日期 2025年04月2025/4/9日、
 * 1 2 3  4 5 6 0
 */
public class Main {

    public static void main(String[] args) {   
            // 创建连表
            Node head = new Node();
            Node tail = null;
            Scanner scanner = new Scanner(System.in);
            int count = createLinkList(head, scanner,tail);
            display(head, count,tail);
            reversal(head);
            display(head, count,tail);
    }


    public static int createLinkList(Node head, Scanner scanner,Node tail) {
        int count = 0;
        Node temp = head;
        int n = scanner.nextInt();
        while (n != 0) {
            temp.next = new Node(n, null);
            tail = temp;
            temp = temp.next;
            n = scanner.nextInt();
            count++;
        }
        tail = head.next;
        return count;
    }

    public static void display(Node head, int count,Node tail) {
        Node next = head.next;
        System.out.println("The length:" + count);
        System.out.println("The elements:");
        while (next != null) {
            if (next == tail){
                break;
            }
            System.out.print(next.data + " ");
            next = next.next;
        }
        System.out.println();
    }

    public static void reversal(Node head){
        Node currNode = head.next; // 当前节点，从链表的第一个节点开始
        Node temp = null; // 临时指针，用于保存下一个节点
        Node node = null; // 前一个节点，初始为null
        while (currNode != null){
            temp = currNode.next;
            currNode.next = node;
            node = currNode;
            currNode = temp;
        }

        head.next = node;
    }
}


class Node {
    public int data;
    public Node next;
    public Node() {
    }

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }
}
